package com.example.zwlyz;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.zwlyz.models.Article;

public class AddArticleActivity extends AppCompatActivity {

    private EditText etTitle;
    private EditText etContent;
    private EditText etAuthor;
    private EditText etTime;
    private Button btnAdd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_article);

        etTitle = findViewById(R.id.etTitle);
        etContent = findViewById(R.id.etContent);
        etAuthor = findViewById(R.id.etAuthor);
        etTime = findViewById(R.id.etTime);
        btnAdd = findViewById(R.id.btnAdd);

        btnAdd.setOnClickListener(v -> {
            String title = etTitle.getText().toString();
            String content = etContent.getText().toString();
            String author = etAuthor.getText().toString();
            String time = etTime.getText().toString();

            if (!title.isEmpty() && !content.isEmpty() && !author.isEmpty() && !time.isEmpty()) {
                // 添加文章到内存
                int id = ArticleManager.getAllArticles().size() + 1;
                Article article = new Article(id, title, content, author, time);
                ArticleManager.addArticle(article);
                Toast.makeText(this, "文章添加成功", Toast.LENGTH_SHORT).show();
                finish();
            } else {
                Toast.makeText(this, "请填写完整信息", Toast.LENGTH_SHORT).show();
            }
        });
    }
}